package com.lex.controller;

import com.lex.common.annoatation.SysLog;
import com.lex.common.api.CommonResult;
import com.lex.common.enums.BusinessType;
import com.lex.entity.account.AddUserDto;
import com.lex.entity.account.UpdateUserPasswordDto;
import com.lex.entity.account.UpdateUserStatusDto;
import com.lex.entity.params.UsersQueryParams;
import com.lex.service.AccountService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/platformAccount")
@Tag(name = "AccountController", description = "平台用户管理接口")
public class AccountController {
    @Resource
    private AccountService accountService;

    @Operation(summary = "获取所有用户列表，带分页")
    @RequestMapping(value = "/getUsersListByPage", method = {RequestMethod.GET})
    public CommonResult getUsersListByPage(UsersQueryParams queryParamDto,
                                           @RequestParam(defaultValue = "1") Integer page,
                                           @RequestParam(defaultValue = "10") Integer pageSize) {
        return accountService.getUsersListByPage(queryParamDto, page, pageSize);
    }
    @Operation(summary = "添加用户")
    @SysLog(title = "添加用户", businessType = BusinessType.INSERT)
    @RequestMapping(value = "/addUser", method = {RequestMethod.POST})
    public CommonResult addUser(@Validated @RequestBody AddUserDto addUserDto) {
        return accountService.addUser(addUserDto);
    }

    @Operation(summary = "判断用户名是否重复")
    @RequestMapping(value = "/isUserNameExist", method = {RequestMethod.GET})
    public CommonResult isUserNameExist(@RequestParam(name = "account",required = true) String userName) {
        return accountService.isUserNameExist(userName);
    }

    @Operation(summary = "删除用户")
    @SysLog(title = "删除用户", businessType = BusinessType.DELETE)
    @RequestMapping(value = "/deleteUser/{userId}", method = {RequestMethod.DELETE})
    public CommonResult deleteUser(@PathVariable Long userId) {
        return accountService.deleteUser(userId);
    }

    @Operation(summary = "修改用户")
    @SysLog(title = "修改用户", businessType = BusinessType.UPDATE)
    @RequestMapping(value = "/updateUser", method = {RequestMethod.PUT})
    public CommonResult updateUser(@Validated @RequestBody AddUserDto addUserDto) {
        return accountService.updateUser(addUserDto);
    }

    @Operation(summary = "修改用户密码")
    @SysLog(title = "修改用户密码", businessType = BusinessType.UPDATE)
    @RequestMapping(value = "/updateUserPassword", method = {RequestMethod.PUT})
    public CommonResult updateUserPassword(@Validated @RequestBody UpdateUserPasswordDto updateUserPasswordDto) {
        return accountService.updateUserPassword(updateUserPasswordDto.getPasswordOld(), updateUserPasswordDto.getPasswordNew());
    }

    @Operation(summary = "修改用户状态")
    @SysLog(title = "修改用户状态", businessType = BusinessType.UPDATE)
    @RequestMapping(value = "/updateUserStatus", method = {RequestMethod.PUT})
    public CommonResult updateUserStatus(@RequestBody UpdateUserStatusDto updateUserStatusDto) {
        return accountService.updateUserStatus(updateUserStatusDto.getUserId(), updateUserStatusDto.getStatus());
    }
}
